package rpg.server;

import java.net.InetAddress;
import java.net.UnknownHostException;
import rpg.server.net.ListenThread;
import rpg.server.util.SleepForever;

/**
 * rothens.tumblr.com
 * @author Rothens
 */
public class RPGServer implements Runnable {

	public static final int VERSION = 8;
	public ListenThread networkServer;
	private boolean serverRunning;

	public RPGServer() {
		serverRunning = true;
		new SleepForever(this);
	}

	public static void main(String[] args) {
		RPGServer rs = new RPGServer();
		rs.run();
	}

	public boolean startServer() throws UnknownHostException {
		InetAddress add = InetAddress.getByName("127.0.0.1");
		int port = 9988;
		try {
			networkServer = new ListenThread(this, add, port);
		} catch (Exception e) {
			System.out.println("Couldn't bind to port!");
			return false;
		}
		return true;
	}

	@Override
	public void run() {
		try {
			if (this.startServer()) {
				long startTick = System.currentTimeMillis();
				for(long tick = 0L; this.serverRunning; Thread.sleep(1L)){
					long time = System.currentTimeMillis();
					long diff = time-startTick;
					tick += diff;
					startTick = time;
					while(tick > 50L) {
						tick -= 50L;
						this.tick();
					}
					
				}
				
			
			}
		} catch (Exception ex){
			
		}
	}
	
	public void tick(){
		//networkServer.handleNetworkListenThread();
		//System.out.println("Tick!");
	}
}
